﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ession.OCR;
using System.Text.RegularExpressions;
using System.Web;
using System.Threading.Tasks;

namespace Ession
{

    public class Reg11GameRobot
    {
        public string IDCard = "230421198501270010";
        public string IDName = "中文";

        private string RegPostDataFormat =
            "__VIEWSTATE={4}&__EVENTVALIDATION={3}&txtEmail={0}&txtSecendPwd={1}&txtSecendPwdSec={1}&txtName=%E4%B8%80%E5%9C%A8%E4%BA%BA&txtCardID=230421198501270010&txtValidateCode={2}&UserCheck=on&imgButtonRegister.x=101&imgButtonRegister.y=20";
        public string Uid { get; set; }
        private string CreatUserURL = "http://register.5211game.com/account/createuser.aspx";

        private string PostDataCreateUser_Format =
            "__VIEWSTATE={0}&__EVENTVALIDATION={1}&txtAccountName={2}&txtPassword=k123123&txtPasswordSec=k123123&txtValidateCode={3}&imgButtonRegister.x=35&imgButtonRegister.y=19";
        private string RegURLFormat = "http://register.5211game.com/register.aspx?spread={0}";
        private string RegValidateURL = "http://register.5211game.com/ValidateCode.aspx";
        private string url1 = "http://www.5211game.com/11/index.html?spread={0}";
        WebClientEss wc;
        Ession.OCR.OcrEssion o = new OcrEssion();
        Regex reg = new Regex(@"id=""__VIEWSTATE"" value=""(.*?)""");
        Regex reg2 = new Regex(@"id=""__EVENTVALIDATION"" value=""(.*?)""");

        public void Register(string userName, string uName, int count, string uid = "361516657")
        {
            if (userName == null || uName == null)
            {
                return;
            }
            wc = new WebClientEss();
            wc.Encoding = Encoding.UTF8;


            string result = "";// wc.DownloadString(string.Format(RegURLFormat, uid));
            while (!result.Contains("成功"))
            {
                result = wc.DownloadString(string.Format(RegURLFormat, uid));
                var viewState = reg.Match(result).Groups[1].ToString();
                var eventV = reg2.Match(result).Groups[1].ToString();

                var imgData = wc.DownloadData(RegValidateURL);

                System.IO.MemoryStream ms = new MemoryStream(imgData);
                Bitmap bmp = new Bitmap(ms);
                Ession.OCR.ImageProcessor5211Game g = new ImageProcessor5211Game(bmp);
                o.P = g;
                var valCode = o.GetText(bmp).Replace("-", "");


                result = wc.PostData(string.Format(RegURLFormat, uid), string.Format(RegPostDataFormat, userName, "123123", valCode, HttpUtility.UrlEncode(eventV), HttpUtility.UrlEncode(viewState))
                    );
                if (result.Contains("该通行证"))
                {
                    File.WriteAllText("name.txt", count.ToString());
                    return;
                }
            }
            File.WriteAllText("name.txt", count.ToString());

            
           
            while (!result.Contains("创建成功"))
            {
                //注册成功,创建账号 
                result = wc.DownloadString(string.Format(CreatUserURL)); 

                var viewState = reg.Match(result).Groups[1].ToString();
                var eventV = reg2.Match(result).Groups[1].ToString();
                var imgData = wc.DownloadData(RegValidateURL);
                System.IO.MemoryStream ms = new MemoryStream(imgData);
                Bitmap bmp = new Bitmap(ms);
                Ession.OCR.ImageProcessor5211Game g = new ImageProcessor5211Game(bmp);
                o.P = g;
                var valCode = o.GetText(bmp).Replace("-", "");

                result = wc.PostData(string.Format(CreatUserURL, uid), string.Format(PostDataCreateUser_Format, HttpUtility.UrlEncode(viewState), HttpUtility.UrlEncode(eventV), uName, valCode)
                    );
            }
            try
            {
                File.AppendAllText("result.txt", userName + "," + uName + Environment.NewLine + DateTime.Now + Environment.NewLine);

            }
            catch (Exception)
            {

            }
        }

        public void BatchRegister(int count, string userEmailFormat = "ess_gar_{0}@1.com", string userIDFormat = "ess_gar_", string uid = "361516657")
        {
            string last;
            try
            {
                last = File.ReadAllText("name.txt");
            }
            catch (Exception ex)
            {
                last = "0";
            }

            Task.Factory.StartNew(
                () =>
                {
                    for (int i = 0; i < count; i++)
                    {
                        Register(string.Format(userEmailFormat, (int.Parse(last) + i)), userIDFormat + (int.Parse(last) + i), int.Parse(last) + i, uid);
                        File.WriteAllText("name.txt", (int.Parse(last) + i).ToString());
                    }
                    OnRegCompleted();
                }
                );

        }
        public event Action RegCompleted;

        public void OnRegCompleted()
        {
            Action handler = RegCompleted;
            if (handler != null) handler();
        }
    }
}
